package com.ttms.service.impl;


import com.ttms.dao.PlayDao;
import com.ttms.dao.impl.PlayDaoImpl;
import com.ttms.service.PlayService;
import com.ttms.dao.PlayLangDao;
import com.ttms.dao.PlayTypeDao;
import com.ttms.dao.impl.PlayLangDaoImpl;
import com.ttms.dao.impl.PlayTypeDaoImpl;
import com.ttms.pojo.Page;
import com.ttms.pojo.Play;
import com.ttms.pojo.PlayLang;
import com.ttms.pojo.PlayType;

import java.util.List;

public class PlayServiceImpl implements PlayService {

    private PlayDao playDao = new PlayDaoImpl();
    private PlayLangDao playLangDao = new PlayLangDaoImpl();
    private PlayTypeDao playTypeDao = new PlayTypeDaoImpl();

    @Override
    public void addPlay(Play play) {
        playDao.addPlay(play);
    }

    @Override
    public void deletePlayById(Integer id) {
        playDao.deletePlayById(id);
    }

    @Override
    public void updatePlay(Play play) {

        playDao.updatePlay(play);
    }

    @Override
    public Play queryPlayById(Integer id) {

        return playDao.queryPlayById(id);
    }

    @Override
    public List<Play> queryPlays() {
        return playDao.queryPlays();
    }

    @Override
    public Page<Play> page(int pageNo, int pageSize) {
        Page<Play> page = new Page<Play>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = playDao.queryForPageTotalCount();
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        // 设置当前页码
        page.setPageNo(pageNo);
        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        // 求当前页数据
        List<Play> items = playDao.queryForPageItems(begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public List<PlayType> queryPlayTypes() {
        return playTypeDao.queryPlayTypes();
    }

    @Override
    public List<PlayLang> queryPlayLangs() {
        return playLangDao.queryPlayLangs();
    }

    @Override
    public Page<Play> queryPlayByName(int pageNo, int pageSize, String play_name) {
        Page<Play> page = new Page<Play>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = playDao.queryPlayCountByName(play_name);
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        // 设置当前页码
        page.setPageNo(pageNo);
        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        // 求当前页数据
        List<Play> items = playDao.queryPlayByName(begin, pageSize, play_name);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }
}